//
//  SignUtils.m
//  KOP
//
//  Created by 王贵源 on 15/9/23.
//  Copyright © 2015年 王贵源. All rights reserved.
//

#import "SignUtils.h"
#import "DGJNilStr.h"
#import "MyMD5.h"

@implementation SignUtils

//新的前面算法
+ (NSString *)signMethod:(NSString *)method ak:(NSString *)ak sk:(NSString *)sk length:(int)length{
    return [MyMD5 md5:[NSString stringWithFormat:@"%@%@%@%d",method,ak,sk,length]];
}
+(NSString *)oldSign:(NSMutableDictionary *)paras fileupload:(bool)fileupload ak:(NSString *)ak sk:(NSString *)sk{
    if (fileupload) {
        NSString * _t = [NSString stringWithFormat:@"%@%@",[self getFirstChar:ak],[self getFirstChar:sk]];
        return [MyMD5 md5:_t ];
    }
    //    NSArray * keys = [paras allKeys];
    NSMutableArray *keys = [NSMutableArray arrayWithArray:[paras allKeys]];
    //    [keys sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(id   obj1, id   obj2) {
    //        NSString *str2 = obj1;
    //        NSString *str1 = obj2;
    //        NSRange compareRang = NSMakeRange(0, 10);
    //        NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch range:compareRang];
    //        return result;
    //    }];
    
    [keys sortWithOptions:NSSortStable usingComparator: ^NSComparisonResult (id obj1, id obj2) {
        NSString *str1 = obj1;
        NSString *str2 = obj2;
        NSRange compareRang = NSMakeRange(0, 1);
        NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch range:compareRang];
        return result;
    }];
    NSString * _t = [self getFirstChar:ak];
    for (int i=0; i < [keys count]; i++) {
        NSString * key = [keys objectAtIndex:i];
        _t = [NSString stringWithFormat:@"%@%@",_t,[self getFirstChar:key]];
    }
    _t = [NSString stringWithFormat:@"%@%@",_t,[self getFirstChar:sk]];
    
    NSLog(@"%@\r\n%@",[MyMD5 md5:_t ],_t);
    return [MyMD5 md5:_t ];
}
+(NSString *) getFirstChar:(NSString *) src{
    if ([DGJNilStr isBlankString:src]) {
        return @"0";
    }
    NSRange range = {0,1};
    
    return [src substringWithRange:range];
}

@end

